/**
 * @author Eddie
 * @date 2023-10-25
 * 值班相关接口(对外提供，例如大屏)
 */

const router = require('koa-router')();
const fs = require('fs');

// 定义路由前缀，没有可不添加
router.prefix('/api/zb');

// 获取介护值班信息
router.get('/getJh', async (ctx, next) => {
  try {
    let jhObj = fs.readFileSync('./files/zz_jh.json','utf8');
    ctx.body = {
      code : 1,
      msg: "读取成功",
      data : JSON.parse(jhObj)
    }
  } catch (error) {
    console.log("getJh", error);
    ctx.body = {
      code : -1,
      msg: "读取失败：" + error,
      data : {}
    }
  }
})

// 添加介护值班
router.post('/addJh', async (ctx, next) => {
  let data = ctx.request.body;
  let re = {};

  try {
    fs.writeFileSync('./files/zz_jh.json', JSON.stringify(data));
    console.log("写入 addJh.js 成功");

    re = {
      code: 1,
      msg: "写入成功"
    }
  } catch (error) {
    console.log("写入失败" + error);

    re = {
      code: -1,
      msg: "写入失败" + error
    }
  }

  ctx.body = re;
})

// 获取医护值班信息
router.get('/getYh', async (ctx, next) => {
  try {
    let jhObj = fs.readFileSync('./files/zz_yh.json','utf8');
    ctx.body = {
      code : 1,
      msg: "读取成功",
      data : JSON.parse(jhObj)
    }
  } catch (error) {
    console.log("getYh", error);
    ctx.body = {
      code : -1,
      msg: "读取失败：" + error,
      data : {}
    }
  }
})

// 添加医护值班
router.post('/addYh', async (ctx, next) => {
  let data = ctx.request.body;
  let re = {};

  try {
    fs.writeFileSync('./files/zz_yh.json', JSON.stringify(data));
    console.log("写入 addYh.js 成功");

    re = {
      code: 1,
      msg: "写入成功"
    }
  } catch (error) {
    console.log("写入失败" + error);

    re = {
      code: -1,
      msg: "写入失败" + error
    }
  }

  ctx.body = re;
})

module.exports = router;